library(foreign)
setwd("~/Replication Code/")

##---Loading in data ----
anes9297 <- read.dta("./data/anes9297_subset.dta")

ccap08 <- read.dta("./data/ccap08_subset.dta")

ccap12 <- read.dta("./data/ccap12_subset.dta")

vsg.dat <- read.dta("./data/vsg_subset.dta")

ccap16 <- read.dta("./data/ccap16_subset.dta")

#----------------------------------------------------------------------------------------------------#
# 1992-1994 ----
#----------------------------------------------------------------------------------------------------#
## Get counts for each of the PID Groups
dem.m <- sum(table(anes9297$pid92_7, anes9297$pid94_7)[2:3,1]) + 
  sum(table(anes9297$pid92_7, anes9297$pid94_7)[3,2])
dem.s <- sum(table(anes9297$pid92_7, anes9297$pid94_7)[1,1]) +
  sum(table(anes9297$pid92_7, anes9297$pid94_7)[2,2]) +
  sum(table(anes9297$pid92_7, anes9297$pid94_7)[3,3])
dem.l <- sum(table(anes9297$pid92_7, anes9297$pid94_7)[1,2:3]) +
  sum(table(anes9297$pid92_7, anes9297$pid94_7)[2,3])
dem.ind <- sum(table(anes9297$pid92_7, anes9297$pid94_7)[1:3,4])
dem.rep <- sum(table(anes9297$pid92_7, anes9297$pid94_7)[1:3,5:7])

ind.d <- sum(table(anes9297$pid92_7, anes9297$pid94_7)[4,1:3])
ind.s <- sum(table(anes9297$pid92_7, anes9297$pid94_7)[4,4])
ind.r <- sum(table(anes9297$pid92_7, anes9297$pid94_7)[4,5:7])

rep.m <- sum(table(anes9297$pid92_7, anes9297$pid94_7)[5,6:7]) + 
  sum(table(anes9297$pid92_7, anes9297$pid94_7)[6,7])
rep.s <- sum(table(anes9297$pid92_7, anes9297$pid94_7)[5,5]) +
  sum(table(anes9297$pid92_7, anes9297$pid94_7)[6,6]) +
  sum(table(anes9297$pid92_7, anes9297$pid94_7)[7,7])
rep.l <- sum(table(anes9297$pid92_7, anes9297$pid94_7)[6:7,5]) +
  sum(table(anes9297$pid92_7, anes9297$pid94_7)[7,6])
rep.ind <- sum(table(anes9297$pid92_7, anes9297$pid94_7)[5:7,4])
rep.dem <- sum(table(anes9297$pid92_7, anes9297$pid94_7)[5:7,1:3])

anes9297$dem.m <- NA
anes9297$dem.m[which(anes9297$pid92_7 < .5)] <- ifelse(anes9297$pid94_7[which(anes9297$pid92_7 < .5)]
                                                        < anes9297$pid92_7[which(anes9297$pid92_7 < .5)], 1, 0)
anes9297$dem.s <- NA
anes9297$dem.s[which(anes9297$pid92_7 < .5)] <- ifelse(anes9297$pid94_7[which(anes9297$pid92_7 < .5)]
                                                        == anes9297$pid92_7[which(anes9297$pid92_7 < .5)], 1, 0)
anes9297$dem.l <- NA
anes9297$dem.l[which(anes9297$pid92_7 < .5)] <- ifelse(anes9297$pid94_7[which(anes9297$pid92_7 < .5)]
                                                        > anes9297$pid92_7[which(anes9297$pid92_7 < .5)] & 
                                                          anes9297$pid94_7[which(anes9297$pid92_7 < .5)] < .5, 1, 0)
anes9297$dem.ind <- NA
anes9297$dem.ind[which(anes9297$pid92_7 < .5)] <- ifelse(anes9297$pid94_7[which(anes9297$pid92_7 < .5)] == .5, 1, 0)
anes9297$dem.rep <- NA
anes9297$dem.rep[which(anes9297$pid92_7 < .5)] <- ifelse(anes9297$pid94_7[which(anes9297$pid92_7 < .5)] > .5, 1, 0)


anes9297$rep.m <- NA
anes9297$rep.m[which(anes9297$pid92_7 > .5)] <- ifelse(anes9297$pid94_7[which(anes9297$pid92_7 > .5)]
                                                        > anes9297$pid92_7[which(anes9297$pid92_7 > .5)], 1, 0)
anes9297$rep.s <- NA
anes9297$rep.s[which(anes9297$pid92_7 > .5)] <- ifelse(anes9297$pid94_7[which(anes9297$pid92_7 > .5)]
                                                        == anes9297$pid92_7[which(anes9297$pid92_7 > .5)], 1, 0)
anes9297$rep.l <- NA
anes9297$rep.l[which(anes9297$pid92_7 > .5)] <- ifelse(anes9297$pid94_7[which(anes9297$pid92_7 > .5)]
                                                        < anes9297$pid92_7[which(anes9297$pid92_7 > .5)] & 
                                                          anes9297$pid94_7[which(anes9297$pid92_7 > .5)] > .5, 1, 0)
anes9297$rep.ind <- NA
anes9297$rep.ind[which(anes9297$pid92_7 > .5)] <- ifelse(anes9297$pid94_7[which(anes9297$pid92_7 > .5)] == .5, 1, 0)
anes9297$rep.dem <- NA
anes9297$rep.dem[which(anes9297$pid92_7 > .5)] <- ifelse(anes9297$pid94_7[which(anes9297$pid92_7 > .5)] < .5, 1, 0)

anes9297$ind.d <- NA
anes9297$ind.d[which(anes9297$pid92_7 == .5)] <- ifelse(anes9297$pid94_7[which(anes9297$pid92_7 == .5)]
                                                         < anes9297$pid92_7[which(anes9297$pid92_7 == .5)], 1, 0)
anes9297$ind.r <- NA
anes9297$ind.r[which(anes9297$pid92_7 == .5)] <- ifelse(anes9297$pid94_7[which(anes9297$pid92_7 == .5)]
                                                         > anes9297$pid92_7[which(anes9297$pid92_7 == .5)], 1, 0)
anes9297$ind.s <- NA
anes9297$ind.s[which(anes9297$pid92_7 == .5)] <- ifelse(anes9297$pid94_7[which(anes9297$pid92_7 == .5)]
                                                         == anes9297$pid92_7[which(anes9297$pid92_7 == .5)], 1, 0)

## Get counts for each of the RR quantiles
anes9297$rr92_scaled6 <- NA
anes9297$rr92_scaled6[which(anes9297$rr92_scaled <= 0.125)] <- 1
anes9297$rr92_scaled6[which(anes9297$rr92_scaled > 0.125 & anes9297$rr92_scaled <= 0.3125)] <- 2
anes9297$rr92_scaled6[which(anes9297$rr92_scaled > 0.3125 & anes9297$rr92_scaled <= 0.5)] <- 3
anes9297$rr92_scaled6[which(anes9297$rr92_scaled > 0.5 & anes9297$rr92_scaled <= 0.6875)] <- 4
anes9297$rr92_scaled6[which(anes9297$rr92_scaled > 0.6875 & anes9297$rr92_scaled <= 0.875)] <- 5
anes9297$rr92_scaled6[which(anes9297$rr92_scaled > 0.875)] <- 6
anes9297$rr94_scaled6 <- NA
anes9297$rr94_scaled6[which(anes9297$rr94_scaled <= 0.125)] <- 1
anes9297$rr94_scaled6[which(anes9297$rr94_scaled > 0.125 & anes9297$rr94_scaled <= 0.3125)] <- 2
anes9297$rr94_scaled6[which(anes9297$rr94_scaled > 0.3125 & anes9297$rr94_scaled <= 0.5)] <- 3
anes9297$rr94_scaled6[which(anes9297$rr94_scaled > 0.5 & anes9297$rr94_scaled <= 0.6875)] <- 4
anes9297$rr94_scaled6[which(anes9297$rr94_scaled > 0.6875 & anes9297$rr94_scaled <= 0.875)] <- 5
anes9297$rr94_scaled6[which(anes9297$rr94_scaled > 0.875)] <- 6

anes9297$rr.s <- NA
anes9297$rr.s <- ifelse((anes9297$rr94_scaled6 == anes9297$rr92_scaled6), 1, 0)
anes9297$rr.l1 <- NA
anes9297$rr.l1 <- ifelse((anes9297$rr94_scaled6 - anes9297$rr92_scaled6) == -1, 1, 0)
anes9297$rr.l2 <- NA
anes9297$rr.l2 <- ifelse((anes9297$rr94_scaled6 - anes9297$rr92_scaled6) == -2, 1, 0)
anes9297$rr.l3 <- NA
anes9297$rr.l3 <- ifelse((anes9297$rr94_scaled6 - anes9297$rr92_scaled6) <= -3, 1, 0)
anes9297$rr.m1 <- NA
anes9297$rr.m1 <- ifelse((anes9297$rr94_scaled6 - anes9297$rr92_scaled6) == 1, 1, 0)
anes9297$rr.m2 <- NA
anes9297$rr.m2 <- ifelse((anes9297$rr94_scaled6 - anes9297$rr92_scaled6) == 2, 1, 0)
anes9297$rr.m3 <- NA
anes9297$rr.m3 <- ifelse((anes9297$rr94_scaled6 - anes9297$rr92_scaled6) >= 3, 1, 0)

### Creating Counts Table
party <- c("dem.m", "ind.d", "rep.dem", "rep.ind", "rep.l", "dem.s", "ind.s", "rep.s", "dem.l", 
           "dem.ind", "dem.rep", "ind.r", "rep.m")
COUNTS <- array(0, c(length(party), 12))
rownames(COUNTS) <- party

DF <- subset(anes9297, white_int92 == white_int94)
for(i in 1:length(party)){
  VAR <- grep(party[i], names(DF), fixed = T)
  D <- DF[which(DF[,VAR] == 1 & DF$rr.s == 1),]
  COUNTS[i,4] <- table(D$rr92_scaled6)["1"]
  COUNTS[i,5] <- table(D$rr92_scaled6)["2"]
  COUNTS[i,6] <- table(D$rr92_scaled6)["3"]
  COUNTS[i,7] <- table(D$rr92_scaled6)["4"]
  COUNTS[i,8] <- table(D$rr92_scaled6)["5"]
  COUNTS[i,9] <- table(D$rr92_scaled6)["6"]
  D <- DF[which(DF[,VAR] == 1 & DF$rr.l1 == 1),]
  COUNTS[i,3] <- nrow(D)
  D <- DF[which(DF[,VAR] == 1 & DF$rr.l2 == 1),]
  COUNTS[i,2] <- nrow(D)
  D <- DF[which(DF[,VAR] == 1 & DF$rr.l3 == 1),]
  COUNTS[i,1] <- nrow(D)
  D <- DF[which(DF[,VAR] == 1 & DF$rr.m1 == 1),]
  COUNTS[i,10] <- nrow(D)
  D <- DF[which(DF[,VAR] == 1 & DF$rr.m2 == 1),]
  COUNTS[i,11] <- nrow(D)
  D <- DF[which(DF[,VAR] == 1 & DF$rr.m3 == 1),]
  COUNTS[i,12] <- nrow(D)
}
COUNTS[which(is.na(COUNTS))] <- 0

#----------------------------------------------------------------------------------------------------#
# CCAP 2008 ----
#----------------------------------------------------------------------------------------------------#
## Get counts for each of the PID Groups
dem.m <- sum(table(ccap08$pid7r_m, ccap08$pid7r_o)[2:3,1]) + 
  sum(table(ccap08$pid7r_m, ccap08$pid7r_o)[3,2])
dem.s <- sum(table(ccap08$pid7r_m, ccap08$pid7r_o)[1,1]) +
  sum(table(ccap08$pid7r_m, ccap08$pid7r_o)[2,2]) +
  sum(table(ccap08$pid7r_m, ccap08$pid7r_o)[3,3])
dem.l <- sum(table(ccap08$pid7r_m, ccap08$pid7r_o)[1,2:3]) +
  sum(table(ccap08$pid7r_m, ccap08$pid7r_o)[2,3])
dem.ind <- sum(table(ccap08$pid7r_m, ccap08$pid7r_o)[1:3,4])
dem.rep <- sum(table(ccap08$pid7r_m, ccap08$pid7r_o)[1:3,5:7])

ind.d <- sum(table(ccap08$pid7r_m, ccap08$pid7r_o)[4,1:3])
ind.s <- sum(table(ccap08$pid7r_m, ccap08$pid7r_o)[4,4])
ind.r <- sum(table(ccap08$pid7r_m, ccap08$pid7r_o)[4,5:7])

rep.m <- sum(table(ccap08$pid7r_m, ccap08$pid7r_o)[5,6:7]) + 
  sum(table(ccap08$pid7r_m, ccap08$pid7r_o)[6,7])
rep.s <- sum(table(ccap08$pid7r_m, ccap08$pid7r_o)[5,5]) +
  sum(table(ccap08$pid7r_m, ccap08$pid7r_o)[6,6]) +
  sum(table(ccap08$pid7r_m, ccap08$pid7r_o)[7,7])
rep.l <- sum(table(ccap08$pid7r_m, ccap08$pid7r_o)[6:7,5]) +
  sum(table(ccap08$pid7r_m, ccap08$pid7r_o)[7,6])
rep.ind <- sum(table(ccap08$pid7r_m, ccap08$pid7r_o)[5:7,4])
rep.dem <- sum(table(ccap08$pid7r_m, ccap08$pid7r_o)[5:7,1:3])

ccap08$dem.m.o <- NA
ccap08$dem.m.o[which(ccap08$pid7r_m < .5)] <- ifelse(ccap08$pid7r_o[which(ccap08$pid7r_m < .5)]
                                                       < ccap08$pid7r_m[which(ccap08$pid7r_m < .5)], 1, 0)
ccap08$dem.s.o <- NA
ccap08$dem.s.o[which(ccap08$pid7r_m < .5)] <- ifelse(ccap08$pid7r_o[which(ccap08$pid7r_m < .5)]
                                                       == ccap08$pid7r_m[which(ccap08$pid7r_m < .5)], 1, 0)
ccap08$dem.l.o <- NA
ccap08$dem.l.o[which(ccap08$pid7r_m < .5)] <- ifelse(ccap08$pid7r_o[which(ccap08$pid7r_m < .5)]
                                                       > ccap08$pid7r_m[which(ccap08$pid7r_m < .5)] & 
                                                         ccap08$pid7r_o[which(ccap08$pid7r_m < .5)] < .5, 1, 0)
ccap08$dem.ind.o <- NA
ccap08$dem.ind.o[which(ccap08$pid7r_m < .5)] <- ifelse(ccap08$pid7r_o[which(ccap08$pid7r_m < .5)] == .5, 1, 0)
ccap08$dem.rep.o <- NA
ccap08$dem.rep.o[which(ccap08$pid7r_m < .5)] <- ifelse(ccap08$pid7r_o[which(ccap08$pid7r_m < .5)] > .5, 1, 0)


ccap08$rep.m.o <- NA
ccap08$rep.m.o[which(ccap08$pid7r_m > .5)] <- ifelse(ccap08$pid7r_o[which(ccap08$pid7r_m > .5)]
                                                       > ccap08$pid7r_m[which(ccap08$pid7r_m > .5)], 1, 0)
ccap08$rep.s.o <- NA
ccap08$rep.s.o[which(ccap08$pid7r_m > .5)] <- ifelse(ccap08$pid7r_o[which(ccap08$pid7r_m > .5)]
                                                       == ccap08$pid7r_m[which(ccap08$pid7r_m > .5)], 1, 0)
ccap08$rep.l.o <- NA
ccap08$rep.l.o[which(ccap08$pid7r_m > .5)] <- ifelse(ccap08$pid7r_o[which(ccap08$pid7r_m > .5)]
                                                       < ccap08$pid7r_m[which(ccap08$pid7r_m > .5)] & 
                                                         ccap08$pid7r_o[which(ccap08$pid7r_m > .5)] > .5, 1, 0)
ccap08$rep.ind.o <- NA
ccap08$rep.ind.o[which(ccap08$pid7r_m > .5)] <- ifelse(ccap08$pid7r_o[which(ccap08$pid7r_m > .5)] == .5, 1, 0)
ccap08$rep.dem.o <- NA
ccap08$rep.dem.o[which(ccap08$pid7r_m > .5)] <- ifelse(ccap08$pid7r_o[which(ccap08$pid7r_m > .5)] < .5, 1, 0)

ccap08$ind.d.o <- NA
ccap08$ind.d.o[which(ccap08$pid7r_m == .5)] <- ifelse(ccap08$pid7r_o[which(ccap08$pid7r_m == .5)]
                                                        < ccap08$pid7r_m[which(ccap08$pid7r_m == .5)], 1, 0)
ccap08$ind.r.o <- NA
ccap08$ind.r.o[which(ccap08$pid7r_m == .5)] <- ifelse(ccap08$pid7r_o[which(ccap08$pid7r_m == .5)]
                                                        > ccap08$pid7r_m[which(ccap08$pid7r_m == .5)], 1, 0)
ccap08$ind.s.o <- NA
ccap08$ind.s.o[which(ccap08$pid7r_m == .5)] <- ifelse(ccap08$pid7r_o[which(ccap08$pid7r_m == .5)]
                                                        == ccap08$pid7r_m[which(ccap08$pid7r_m == .5)], 1, 0)


## Get counts for each of the RR quantiles
ccap08$rr_m_scaled6 <- NA
ccap08$rr_m_scaled6[which(ccap08$rr_m_scaled <= 0.125)] <- 1
ccap08$rr_m_scaled6[which(ccap08$rr_m_scaled > 0.125 & ccap08$rr_m_scaled <= 0.3125)] <- 2
ccap08$rr_m_scaled6[which(ccap08$rr_m_scaled > 0.3125 & ccap08$rr_m_scaled <= 0.5)] <- 3
ccap08$rr_m_scaled6[which(ccap08$rr_m_scaled > 0.5 & ccap08$rr_m_scaled <= 0.6875)] <- 4
ccap08$rr_m_scaled6[which(ccap08$rr_m_scaled > 0.6875 & ccap08$rr_m_scaled <= 0.875)] <- 5
ccap08$rr_m_scaled6[which(ccap08$rr_m_scaled > 0.875)] <- 6
ccap08$rr_o_scaled6 <- NA
ccap08$rr_o_scaled6[which(ccap08$rr_o_scaled <= 0.125)] <- 1
ccap08$rr_o_scaled6[which(ccap08$rr_o_scaled > 0.125 & ccap08$rr_o_scaled <= 0.3125)] <- 2
ccap08$rr_o_scaled6[which(ccap08$rr_o_scaled > 0.3125 & ccap08$rr_o_scaled <= 0.5)] <- 3
ccap08$rr_o_scaled6[which(ccap08$rr_o_scaled > 0.5 & ccap08$rr_o_scaled <= 0.6875)] <- 4
ccap08$rr_o_scaled6[which(ccap08$rr_o_scaled > 0.6875 & ccap08$rr_o_scaled <= 0.875)] <- 5
ccap08$rr_o_scaled6[which(ccap08$rr_o_scaled > 0.875)] <- 6

ccap08$rr.s.o <- NA
ccap08$rr.s.o <- ifelse(ccap08$rr_o_scaled6 == ccap08$rr_m_scaled6, 1, 0)
ccap08$rr.l1.o <- NA
ccap08$rr.l1.o <- ifelse((ccap08$rr_o_scaled6 - ccap08$rr_m_scaled6) == -1, 1, 0)
ccap08$rr.l2.o <- NA
ccap08$rr.l2.o <- ifelse((ccap08$rr_o_scaled6 - ccap08$rr_m_scaled6) == -2, 1, 0)
ccap08$rr.l3.o <- NA
ccap08$rr.l3.o <- ifelse((ccap08$rr_o_scaled6 - ccap08$rr_m_scaled6) <= -3, 1, 0)
ccap08$rr.m.o <- NA
ccap08$rr.m.o <- ifelse((ccap08$rr_o_scaled6 - ccap08$rr_m_scaled6) == 1, 1, 0)
ccap08$rr.m2.o <- NA
ccap08$rr.m2.o <- ifelse((ccap08$rr_o_scaled6 - ccap08$rr_m_scaled6) == 2, 1, 0)
ccap08$rr.m3.o <- NA
ccap08$rr.m3.o <- ifelse((ccap08$rr_o_scaled6 - ccap08$rr_m_scaled6) >= 3, 1, 0)

### Creating Counts Table
party <- c("dem.m", "ind.d", "rep.dem", "rep.ind", "rep.l", "dem.s", "ind.s", "rep.s", "dem.l", 
           "dem.ind", "dem.rep", "ind.r", "rep.m")
# Takes Care of rows 1:6; iterate over "party" vector
COUNTS <- array(0, c(length(party), 12))
rownames(COUNTS) <- party
DF <- ccap08
for(i in 1:length(party)){
  VAR <- grep(paste(party[i], ".o", sep = ""), names(DF))
  D <- DF[which(DF[,VAR] == 1 & DF$rr.s.o == 1),]
  COUNTS[i,4] <- table(D$rr_m_scaled6)["1"]
  COUNTS[i,5] <- table(D$rr_m_scaled6)["2"]
  COUNTS[i,6] <- table(D$rr_m_scaled6)["3"]
  COUNTS[i,7] <- table(D$rr_m_scaled6)["4"]
  COUNTS[i,8] <- table(D$rr_m_scaled6)["5"]
  COUNTS[i,9] <- table(D$rr_m_scaled6)["6"]
  D <- DF[which(DF[,VAR] == 1 & DF$rr.l1.o == 1),]
  COUNTS[i,3] <- nrow(D)
  D <- DF[which(DF[,VAR] == 1 & DF$rr.l2.o == 1),]
  COUNTS[i,2] <- nrow(D)
  D <- DF[which(DF[,VAR] == 1 & DF$rr.l3.o == 1),]
  COUNTS[i,1] <- nrow(D)
  D <- DF[which(DF[,VAR] == 1 & DF$rr.m.o == 1),]
  COUNTS[i,10] <- nrow(D)
  D <- DF[which(DF[,VAR] == 1 & DF$rr.m2.o == 1),]
  COUNTS[i,11] <- nrow(D)
  D <- DF[which(DF[,VAR] == 1 & DF$rr.m3.o == 1),]
  COUNTS[i,12] <- nrow(D)
}
COUNTS[which(is.na(COUNTS))] <- 0

#----------------------------------------------------------------------------------------------------#
# CCAP 2012: March ----
#----------------------------------------------------------------------------------------------------#
## Get counts for each of the PID Groups
ccap12.m <- subset(ccap12, p2 == 1)
dem.m <- sum(table(ccap12.m$pp_pid7r_sc, ccap12.m$pid7_sc)[2:3,1]) + 
  sum(table(ccap12.m$pp_pid7r_sc, ccap12.m$pid7_sc)[3,2])
dem.s <- sum(table(ccap12.m$pp_pid7r_sc, ccap12.m$pid7_sc)[1,1]) +
  sum(table(ccap12.m$pp_pid7r_sc, ccap12.m$pid7_sc)[2,2]) +
  sum(table(ccap12.m$pp_pid7r_sc, ccap12.m$pid7_sc)[3,3])
dem.l <- sum(table(ccap12.m$pp_pid7r_sc, ccap12.m$pid7_sc)[1,2:3]) +
  sum(table(ccap12.m$pp_pid7r_sc, ccap12.m$pid7_sc)[2,3])
dem.ind <- sum(table(ccap12.m$pp_pid7r_sc, ccap12.m$pid7_sc)[1:3,4])
dem.rep <- sum(table(ccap12.m$pp_pid7r_sc, ccap12.m$pid7_sc)[1:3,5:7])

ind.d <- sum(table(ccap12.m$pp_pid7r_sc, ccap12.m$pid7_sc)[4,1:3])
ind.s <- sum(table(ccap12.m$pp_pid7r_sc, ccap12.m$pid7_sc)[4,4])
ind.r <- sum(table(ccap12.m$pp_pid7r_sc, ccap12.m$pid7_sc)[4,5:7])

rep.m <- sum(table(ccap12.m$pp_pid7r_sc, ccap12.m$pid7_sc)[5,6:7]) + 
  sum(table(ccap12.m$pp_pid7r_sc, ccap12.m$pid7_sc)[6,7])
rep.s <- sum(table(ccap12.m$pp_pid7r_sc, ccap12.m$pid7_sc)[5,5]) +
  sum(table(ccap12.m$pp_pid7r_sc, ccap12.m$pid7_sc)[6,6]) +
  sum(table(ccap12.m$pp_pid7r_sc, ccap12.m$pid7_sc)[7,7])
rep.l <- sum(table(ccap12.m$pp_pid7r_sc, ccap12.m$pid7_sc)[6:7,5]) +
  sum(table(ccap12.m$pp_pid7r_sc, ccap12.m$pid7_sc)[7,6])
rep.ind <- sum(table(ccap12.m$pp_pid7r_sc, ccap12.m$pid7_sc)[5:7,4])
rep.dem <- sum(table(ccap12.m$pp_pid7r_sc, ccap12.m$pid7_sc)[5:7,1:3])

ccap12.m$dem.m <- NA
ccap12.m$dem.m[which(ccap12.m$pp_pid7r_sc < .5)] <- ifelse(ccap12.m$pid7_sc[which(ccap12.m$pp_pid7r_sc < .5)]
                                                          < ccap12.m$pp_pid7r_sc[which(ccap12.m$pp_pid7r_sc < .5)], 1, 0)
ccap12.m$dem.s <- NA
ccap12.m$dem.s[which(ccap12.m$pp_pid7r_sc < .5)] <- ifelse(ccap12.m$pid7_sc[which(ccap12.m$pp_pid7r_sc < .5)]
                                                          == ccap12.m$pp_pid7r_sc[which(ccap12.m$pp_pid7r_sc < .5)], 1, 0)
ccap12.m$dem.l <- NA
ccap12.m$dem.l[which(ccap12.m$pp_pid7r_sc < .5)] <- ifelse(ccap12.m$pid7_sc[which(ccap12.m$pp_pid7r_sc < .5)]
                                                          > ccap12.m$pp_pid7r_sc[which(ccap12.m$pp_pid7r_sc < .5)] & 
                                                            ccap12.m$pid7_sc[which(ccap12.m$pp_pid7r_sc < .5)] < .5, 1, 0)
ccap12.m$dem.ind <- NA
ccap12.m$dem.ind[which(ccap12.m$pp_pid7r_sc < .5)] <- ifelse(ccap12.m$pid7_sc[which(ccap12.m$pp_pid7r_sc < .5)] == .5, 1, 0)
ccap12.m$dem.rep <- NA
ccap12.m$dem.rep[which(ccap12.m$pp_pid7r_sc < .5)] <- ifelse(ccap12.m$pid7_sc[which(ccap12.m$pp_pid7r_sc < .5)] > .5, 1, 0)


ccap12.m$rep.m <- NA
ccap12.m$rep.m[which(ccap12.m$pp_pid7r_sc > .5)] <- ifelse(ccap12.m$pid7_sc[which(ccap12.m$pp_pid7r_sc > .5)]
                                                          > ccap12.m$pp_pid7r_sc[which(ccap12.m$pp_pid7r_sc > .5)], 1, 0)
ccap12.m$rep.s <- NA
ccap12.m$rep.s[which(ccap12.m$pp_pid7r_sc > .5)] <- ifelse(ccap12.m$pid7_sc[which(ccap12.m$pp_pid7r_sc > .5)]
                                                          == ccap12.m$pp_pid7r_sc[which(ccap12.m$pp_pid7r_sc > .5)], 1, 0)
ccap12.m$rep.l <- NA
ccap12.m$rep.l[which(ccap12.m$pp_pid7r_sc > .5)] <- ifelse(ccap12.m$pid7_sc[which(ccap12.m$pp_pid7r_sc > .5)]
                                                          < ccap12.m$pp_pid7r_sc[which(ccap12.m$pp_pid7r_sc > .5)] & 
                                                            ccap12.m$pid7_sc[which(ccap12.m$pp_pid7r_sc > .5)] > .5, 1, 0)
ccap12.m$rep.ind <- NA
ccap12.m$rep.ind[which(ccap12.m$pp_pid7r_sc > .5)] <- ifelse(ccap12.m$pid7_sc[which(ccap12.m$pp_pid7r_sc > .5)] == .5, 1, 0)
ccap12.m$rep.dem <- NA
ccap12.m$rep.dem[which(ccap12.m$pp_pid7r_sc > .5)] <- ifelse(ccap12.m$pid7_sc[which(ccap12.m$pp_pid7r_sc > .5)] < .5, 1, 0)

ccap12.m$ind.d <- NA
ccap12.m$ind.d[which(ccap12.m$pp_pid7r_sc == .5)] <- ifelse(ccap12.m$pid7_sc[which(ccap12.m$pp_pid7r_sc == .5)]
                                                           < ccap12.m$pp_pid7r_sc[which(ccap12.m$pp_pid7r_sc == .5)], 1, 0)
ccap12.m$ind.r <- NA
ccap12.m$ind.r[which(ccap12.m$pp_pid7r_sc == .5)] <- ifelse(ccap12.m$pid7_sc[which(ccap12.m$pp_pid7r_sc == .5)]
                                                           > ccap12.m$pp_pid7r_sc[which(ccap12.m$pp_pid7r_sc == .5)], 1, 0)
ccap12.m$ind.s <- NA
ccap12.m$ind.s[which(ccap12.m$pp_pid7r_sc == .5)] <- ifelse(ccap12.m$pid7_sc[which(ccap12.m$pp_pid7r_sc == .5)]
                                                           == ccap12.m$pp_pid7r_sc[which(ccap12.m$pp_pid7r_sc == .5)], 1, 0)


## Get counts for each of the RR quantiles
ccap12.m$rr_p1_scaled6 <- NA
ccap12.m$rr_p1_scaled6[which(ccap12.m$rr_p1_scaled <= 0.125)] <- 1
ccap12.m$rr_p1_scaled6[which(ccap12.m$rr_p1_scaled > 0.125 & ccap12.m$rr_p1_scaled <= 0.3125)] <- 2
ccap12.m$rr_p1_scaled6[which(ccap12.m$rr_p1_scaled > 0.3125 & ccap12.m$rr_p1_scaled <= 0.5)] <- 3
ccap12.m$rr_p1_scaled6[which(ccap12.m$rr_p1_scaled > 0.5 & ccap12.m$rr_p1_scaled <= 0.6875)] <- 4
ccap12.m$rr_p1_scaled6[which(ccap12.m$rr_p1_scaled > 0.6875 & ccap12.m$rr_p1_scaled <= 0.875)] <- 5
ccap12.m$rr_p1_scaled6[which(ccap12.m$rr_p1_scaled > 0.875)] <- 6
ccap12.m$rr_p2_scaled6 <- NA
ccap12.m$rr_p2_scaled6[which(ccap12.m$rr_p2_scaled <= 0.125)] <- 1
ccap12.m$rr_p2_scaled6[which(ccap12.m$rr_p2_scaled > 0.125 & ccap12.m$rr_p2_scaled <= 0.3125)] <- 2
ccap12.m$rr_p2_scaled6[which(ccap12.m$rr_p2_scaled > 0.3125 & ccap12.m$rr_p2_scaled <= 0.5)] <- 3
ccap12.m$rr_p2_scaled6[which(ccap12.m$rr_p2_scaled > 0.5 & ccap12.m$rr_p2_scaled <= 0.6875)] <- 4
ccap12.m$rr_p2_scaled6[which(ccap12.m$rr_p2_scaled > 0.6875 & ccap12.m$rr_p2_scaled <= 0.875)] <- 5
ccap12.m$rr_p2_scaled6[which(ccap12.m$rr_p2_scaled > 0.875)] <- 6

ccap12.m$rr.s <- NA
ccap12.m$rr.s <- ifelse(ccap12.m$rr_p2_scaled6 == ccap12.m$rr_p1_scaled6, 1, 0)
ccap12.m$rr.l1 <- NA
ccap12.m$rr.l1 <- ifelse((ccap12.m$rr_p2_scaled6 - ccap12.m$rr_p1_scaled6) == -1, 1, 0)
ccap12.m$rr.l2 <- NA
ccap12.m$rr.l2 <- ifelse((ccap12.m$rr_p2_scaled6 - ccap12.m$rr_p1_scaled6) == -2, 1, 0)
ccap12.m$rr.l3 <- NA
ccap12.m$rr.l3 <- ifelse((ccap12.m$rr_p2_scaled6 - ccap12.m$rr_p1_scaled6) <= -3, 1, 0)
ccap12.m$rr.m1 <- NA
ccap12.m$rr.m1 <- ifelse((ccap12.m$rr_p2_scaled6 - ccap12.m$rr_p1_scaled6) == 1, 1, 0)
ccap12.m$rr.m2 <- NA
ccap12.m$rr.m2 <- ifelse((ccap12.m$rr_p2_scaled6 - ccap12.m$rr_p1_scaled6) == 2, 1, 0)
ccap12.m$rr.m3 <- NA
ccap12.m$rr.m3 <- ifelse((ccap12.m$rr_p2_scaled6 - ccap12.m$rr_p1_scaled6) >= 3, 1, 0)

### Creating Counts Table
party <- c("dem.m", "ind.d", "rep.dem", "rep.ind", "rep.l", "dem.s", "ind.s", "rep.s", "dem.l", 
           "dem.ind", "dem.rep", "ind.r", "rep.m")
# Takes Care of rows 1:6; iterate over "party" vector
COUNTS <- array(0, c(length(party), 12))
rownames(COUNTS) <- party
DF <- subset(ccap12.m)
for(i in 1:length(party)){
  VAR <- grep(party[i], names(DF), fixed = T)
  D <- DF[which(DF[,VAR] == 1 & DF$rr.s == 1),]
  COUNTS[i,4] <- table(D$rr_p1_scaled6)["1"]
  COUNTS[i,5] <- table(D$rr_p1_scaled6)["2"]
  COUNTS[i,6] <- table(D$rr_p1_scaled6)["3"]
  COUNTS[i,7] <- table(D$rr_p1_scaled6)["4"]
  COUNTS[i,8] <- table(D$rr_p1_scaled6)["5"]
  COUNTS[i,9] <- table(D$rr_p1_scaled6)["6"]
  D <- DF[which(DF[,VAR] == 1 & DF$rr.l1 == 1),]
  COUNTS[i,3] <- nrow(D)
  D <- DF[which(DF[,VAR] == 1 & DF$rr.l2 == 1),]
  COUNTS[i,2] <- nrow(D)
  D <- DF[which(DF[,VAR] == 1 & DF$rr.l3 == 1),]
  COUNTS[i,1] <- nrow(D)
  D <- DF[which(DF[,VAR] == 1 & DF$rr.m1 == 1),]
  COUNTS[i,10] <- nrow(D)
  D <- DF[which(DF[,VAR] == 1 & DF$rr.m2 == 1),]
  COUNTS[i,11] <- nrow(D)
  D <- DF[which(DF[,VAR] == 1 & DF$rr.m3 == 1),]
  COUNTS[i,12] <- nrow(D)
}
COUNTS[which(is.na(COUNTS))] <- 0

#----------------------------------------------------------------------------------------------------#
# CCAP 2012: August ----
#----------------------------------------------------------------------------------------------------#
## Get counts for each of the PID Groups
ccap12.a <- subset(ccap12, p3 == 1)
dem.m <- sum(table(ccap12.a$pp_pid7r_sc, ccap12.a$pid7_sc)[2:3,1]) + 
  sum(table(ccap12.a$pp_pid7r_sc, ccap12.a$pid7_sc)[3,2])
dem.s <- sum(table(ccap12.a$pp_pid7r_sc, ccap12.a$pid7_sc)[1,1]) +
  sum(table(ccap12.a$pp_pid7r_sc, ccap12.a$pid7_sc)[2,2]) +
  sum(table(ccap12.a$pp_pid7r_sc, ccap12.a$pid7_sc)[3,3])
dem.l <- sum(table(ccap12.a$pp_pid7r_sc, ccap12.a$pid7_sc)[1,2:3]) +
  sum(table(ccap12.a$pp_pid7r_sc, ccap12.a$pid7_sc)[2,3])
dem.ind <- sum(table(ccap12.a$pp_pid7r_sc, ccap12.a$pid7_sc)[1:3,4])
dem.rep <- sum(table(ccap12.a$pp_pid7r_sc, ccap12.a$pid7_sc)[1:3,5:7])

ind.d <- sum(table(ccap12.a$pp_pid7r_sc, ccap12.a$pid7_sc)[4,1:3])
ind.s <- sum(table(ccap12.a$pp_pid7r_sc, ccap12.a$pid7_sc)[4,4])
ind.r <- sum(table(ccap12.a$pp_pid7r_sc, ccap12.a$pid7_sc)[4,5:7])

rep.m <- sum(table(ccap12.a$pp_pid7r_sc, ccap12.a$pid7_sc)[5,6:7]) + 
  sum(table(ccap12.a$pp_pid7r_sc, ccap12.a$pid7_sc)[6,7])
rep.s <- sum(table(ccap12.a$pp_pid7r_sc, ccap12.a$pid7_sc)[5,5]) +
  sum(table(ccap12.a$pp_pid7r_sc, ccap12.a$pid7_sc)[6,6]) +
  sum(table(ccap12.a$pp_pid7r_sc, ccap12.a$pid7_sc)[7,7])
rep.l <- sum(table(ccap12.a$pp_pid7r_sc, ccap12.a$pid7_sc)[6:7,5]) +
  sum(table(ccap12.a$pp_pid7r_sc, ccap12.a$pid7_sc)[7,6])
rep.ind <- sum(table(ccap12.a$pp_pid7r_sc, ccap12.a$pid7_sc)[5:7,4])
rep.dem <- sum(table(ccap12.a$pp_pid7r_sc, ccap12.a$pid7_sc)[5:7,1:3])

ccap12.a$dem.m <- NA
ccap12.a$dem.m[which(ccap12.a$pp_pid7r_sc < .5)] <- ifelse(ccap12.a$pid7_sc[which(ccap12.a$pp_pid7r_sc < .5)]
                                                           < ccap12.a$pp_pid7r_sc[which(ccap12.a$pp_pid7r_sc < .5)], 1, 0)
ccap12.a$dem.s <- NA
ccap12.a$dem.s[which(ccap12.a$pp_pid7r_sc < .5)] <- ifelse(ccap12.a$pid7_sc[which(ccap12.a$pp_pid7r_sc < .5)]
                                                           == ccap12.a$pp_pid7r_sc[which(ccap12.a$pp_pid7r_sc < .5)], 1, 0)
ccap12.a$dem.l <- NA
ccap12.a$dem.l[which(ccap12.a$pp_pid7r_sc < .5)] <- ifelse(ccap12.a$pid7_sc[which(ccap12.a$pp_pid7r_sc < .5)]
                                                           > ccap12.a$pp_pid7r_sc[which(ccap12.a$pp_pid7r_sc < .5)] & 
                                                             ccap12.a$pid7_sc[which(ccap12.a$pp_pid7r_sc < .5)] < .5, 1, 0)
ccap12.a$dem.ind <- NA
ccap12.a$dem.ind[which(ccap12.a$pp_pid7r_sc < .5)] <- ifelse(ccap12.a$pid7_sc[which(ccap12.a$pp_pid7r_sc < .5)] == .5, 1, 0)
ccap12.a$dem.rep <- NA
ccap12.a$dem.rep[which(ccap12.a$pp_pid7r_sc < .5)] <- ifelse(ccap12.a$pid7_sc[which(ccap12.a$pp_pid7r_sc < .5)] > .5, 1, 0)


ccap12.a$rep.m <- NA
ccap12.a$rep.m[which(ccap12.a$pp_pid7r_sc > .5)] <- ifelse(ccap12.a$pid7_sc[which(ccap12.a$pp_pid7r_sc > .5)]
                                                           > ccap12.a$pp_pid7r_sc[which(ccap12.a$pp_pid7r_sc > .5)], 1, 0)
ccap12.a$rep.s <- NA
ccap12.a$rep.s[which(ccap12.a$pp_pid7r_sc > .5)] <- ifelse(ccap12.a$pid7_sc[which(ccap12.a$pp_pid7r_sc > .5)]
                                                           == ccap12.a$pp_pid7r_sc[which(ccap12.a$pp_pid7r_sc > .5)], 1, 0)
ccap12.a$rep.l <- NA
ccap12.a$rep.l[which(ccap12.a$pp_pid7r_sc > .5)] <- ifelse(ccap12.a$pid7_sc[which(ccap12.a$pp_pid7r_sc > .5)]
                                                           < ccap12.a$pp_pid7r_sc[which(ccap12.a$pp_pid7r_sc > .5)] & 
                                                             ccap12.a$pid7_sc[which(ccap12.a$pp_pid7r_sc > .5)] > .5, 1, 0)
ccap12.a$rep.ind <- NA
ccap12.a$rep.ind[which(ccap12.a$pp_pid7r_sc > .5)] <- ifelse(ccap12.a$pid7_sc[which(ccap12.a$pp_pid7r_sc > .5)] == .5, 1, 0)
ccap12.a$rep.dem <- NA
ccap12.a$rep.dem[which(ccap12.a$pp_pid7r_sc > .5)] <- ifelse(ccap12.a$pid7_sc[which(ccap12.a$pp_pid7r_sc > .5)] < .5, 1, 0)

ccap12.a$ind.d <- NA
ccap12.a$ind.d[which(ccap12.a$pp_pid7r_sc == .5)] <- ifelse(ccap12.a$pid7_sc[which(ccap12.a$pp_pid7r_sc == .5)]
                                                            < ccap12.a$pp_pid7r_sc[which(ccap12.a$pp_pid7r_sc == .5)], 1, 0)
ccap12.a$ind.r <- NA
ccap12.a$ind.r[which(ccap12.a$pp_pid7r_sc == .5)] <- ifelse(ccap12.a$pid7_sc[which(ccap12.a$pp_pid7r_sc == .5)]
                                                            > ccap12.a$pp_pid7r_sc[which(ccap12.a$pp_pid7r_sc == .5)], 1, 0)
ccap12.a$ind.s <- NA
ccap12.a$ind.s[which(ccap12.a$pp_pid7r_sc == .5)] <- ifelse(ccap12.a$pid7_sc[which(ccap12.a$pp_pid7r_sc == .5)]
                                                            == ccap12.a$pp_pid7r_sc[which(ccap12.a$pp_pid7r_sc == .5)], 1, 0)


## Get counts for each of the RR quantiles
ccap12.a$rr_p1_scaled6 <- NA
ccap12.a$rr_p1_scaled6[which(ccap12.a$rr_p1_scaled <= 0.125)] <- 1
ccap12.a$rr_p1_scaled6[which(ccap12.a$rr_p1_scaled > 0.125 & ccap12.a$rr_p1_scaled <= 0.3125)] <- 2
ccap12.a$rr_p1_scaled6[which(ccap12.a$rr_p1_scaled > 0.3125 & ccap12.a$rr_p1_scaled <= 0.5)] <- 3
ccap12.a$rr_p1_scaled6[which(ccap12.a$rr_p1_scaled > 0.5 & ccap12.a$rr_p1_scaled <= 0.6875)] <- 4
ccap12.a$rr_p1_scaled6[which(ccap12.a$rr_p1_scaled > 0.6875 & ccap12.a$rr_p1_scaled <= 0.875)] <- 5
ccap12.a$rr_p1_scaled6[which(ccap12.a$rr_p1_scaled > 0.875)] <- 6
ccap12.a$rr_p3_scaled6 <- NA
ccap12.a$rr_p3_scaled6[which(ccap12.a$rr_p3_scaled <= 0.125)] <- 1
ccap12.a$rr_p3_scaled6[which(ccap12.a$rr_p3_scaled > 0.125 & ccap12.a$rr_p3_scaled <= 0.3125)] <- 2
ccap12.a$rr_p3_scaled6[which(ccap12.a$rr_p3_scaled > 0.3125 & ccap12.a$rr_p3_scaled <= 0.5)] <- 3
ccap12.a$rr_p3_scaled6[which(ccap12.a$rr_p3_scaled > 0.5 & ccap12.a$rr_p3_scaled <= 0.6875)] <- 4
ccap12.a$rr_p3_scaled6[which(ccap12.a$rr_p3_scaled > 0.6875 & ccap12.a$rr_p3_scaled <= 0.875)] <- 5
ccap12.a$rr_p3_scaled6[which(ccap12.a$rr_p3_scaled > 0.875)] <- 6

ccap12.a$rr.s <- NA
ccap12.a$rr.s <- ifelse(ccap12.a$rr_p3_scaled6 == ccap12.a$rr_p1_scaled6, 1, 0)
ccap12.a$rr.l1 <- NA
ccap12.a$rr.l1 <- ifelse((ccap12.a$rr_p3_scaled6 - ccap12.a$rr_p1_scaled6) == -1, 1, 0)
ccap12.a$rr.l2 <- NA
ccap12.a$rr.l2 <- ifelse((ccap12.a$rr_p3_scaled6 - ccap12.a$rr_p1_scaled6) == -2, 1, 0)
ccap12.a$rr.l3 <- NA
ccap12.a$rr.l3 <- ifelse((ccap12.a$rr_p3_scaled6 - ccap12.a$rr_p1_scaled6) <= -3, 1, 0)
ccap12.a$rr.m1 <- NA
ccap12.a$rr.m1 <- ifelse((ccap12.a$rr_p3_scaled6 - ccap12.a$rr_p1_scaled6) == 1, 1, 0)
ccap12.a$rr.m2 <- NA
ccap12.a$rr.m2 <- ifelse((ccap12.a$rr_p3_scaled6 - ccap12.a$rr_p1_scaled6) == 2, 1, 0)
ccap12.a$rr.m3 <- NA
ccap12.a$rr.m3 <- ifelse((ccap12.a$rr_p3_scaled6 - ccap12.a$rr_p1_scaled6) >= 3, 1, 0)

### Creating Counts Table
party <- c("dem.m", "ind.d", "rep.dem", "rep.ind", "rep.l", "dem.s", "ind.s", "rep.s", "dem.l", 
           "dem.ind", "dem.rep", "ind.r", "rep.m")
# Takes Care of rows 1:6; iterate over "party" vector
COUNTS <- array(0, c(length(party), 12))
rownames(COUNTS) <- party
DF <- subset(ccap12.a)
for(i in 1:length(party)){
  VAR <- grep(party[i], names(DF), fixed = T)
  D <- DF[which(DF[,VAR] == 1 & DF$rr.s == 1),]
  COUNTS[i,4] <- table(D$rr_p1_scaled6)["1"]
  COUNTS[i,5] <- table(D$rr_p1_scaled6)["2"]
  COUNTS[i,6] <- table(D$rr_p1_scaled6)["3"]
  COUNTS[i,7] <- table(D$rr_p1_scaled6)["4"]
  COUNTS[i,8] <- table(D$rr_p1_scaled6)["5"]
  COUNTS[i,9] <- table(D$rr_p1_scaled6)["6"]
  D <- DF[which(DF[,VAR] == 1 & DF$rr.l1 == 1),]
  COUNTS[i,3] <- nrow(D)
  D <- DF[which(DF[,VAR] == 1 & DF$rr.l2 == 1),]
  COUNTS[i,2] <- nrow(D)
  D <- DF[which(DF[,VAR] == 1 & DF$rr.l3 == 1),]
  COUNTS[i,1] <- nrow(D)
  D <- DF[which(DF[,VAR] == 1 & DF$rr.m1 == 1),]
  COUNTS[i,10] <- nrow(D)
  D <- DF[which(DF[,VAR] == 1 & DF$rr.m2 == 1),]
  COUNTS[i,11] <- nrow(D)
  D <- DF[which(DF[,VAR] == 1 & DF$rr.m3 == 1),]
  COUNTS[i,12] <- nrow(D)
}
COUNTS[which(is.na(COUNTS))] <- 0

#----------------------------------------------------------------------------------------------------#
# VSG 2012-2016 ----
#----------------------------------------------------------------------------------------------------#
dem.m <- sum(table(vsg.dat$pid7_sc_12, vsg.dat$pid7_sc_16)[2:3,1]) + 
  sum(table(vsg.dat$pid7_sc_12, vsg.dat$pid7_sc_16)[3,2])
dem.s <- sum(table(vsg.dat$pid7_sc_12, vsg.dat$pid7_sc_16)[1,1]) +
  sum(table(vsg.dat$pid7_sc_12, vsg.dat$pid7_sc_16)[2,2]) +
  sum(table(vsg.dat$pid7_sc_12, vsg.dat$pid7_sc_16)[3,3])
dem.l <- sum(table(vsg.dat$pid7_sc_12, vsg.dat$pid7_sc_16)[1,2:3]) +
  sum(table(vsg.dat$pid7_sc_12, vsg.dat$pid7_sc_16)[2,3])
dem.ind <- sum(table(vsg.dat$pid7_sc_12, vsg.dat$pid7_sc_16)[1:3,4])
dem.rep <- sum(table(vsg.dat$pid7_sc_12, vsg.dat$pid7_sc_16)[1:3,5:7])

ind.d <- sum(table(vsg.dat$pid7_sc_12, vsg.dat$pid7_sc_16)[4,1:3])
ind.s <- sum(table(vsg.dat$pid7_sc_12, vsg.dat$pid7_sc_16)[4,4])
ind.r <- sum(table(vsg.dat$pid7_sc_12, vsg.dat$pid7_sc_16)[4,5:7])

rep.m <- sum(table(vsg.dat$pid7_sc_12, vsg.dat$pid7_sc_16)[5,6:7]) + 
  sum(table(vsg.dat$pid7_sc_12, vsg.dat$pid7_sc_16)[6,7])
rep.s <- sum(table(vsg.dat$pid7_sc_12, vsg.dat$pid7_sc_16)[5,5]) +
  sum(table(vsg.dat$pid7_sc_12, vsg.dat$pid7_sc_16)[6,6]) +
  sum(table(vsg.dat$pid7_sc_12, vsg.dat$pid7_sc_16)[7,7])
rep.l <- sum(table(vsg.dat$pid7_sc_12, vsg.dat$pid7_sc_16)[6:7,5]) +
  sum(table(vsg.dat$pid7_sc_12, vsg.dat$pid7_sc_16)[7,6])
rep.ind <- sum(table(vsg.dat$pid7_sc_12, vsg.dat$pid7_sc_16)[5:7,4])
rep.dem <- sum(table(vsg.dat$pid7_sc_12, vsg.dat$pid7_sc_16)[5:7,1:3])

vsg.dat$dem.m <- NA
vsg.dat$dem.m[which(vsg.dat$pid7_sc_12 < .5)] <- ifelse(vsg.dat$pid7_sc_16[which(vsg.dat$pid7_sc_12 < .5)]
                                                      < vsg.dat$pid7_sc_12[which(vsg.dat$pid7_sc_12 < .5)], 1, 0)
vsg.dat$dem.s <- NA
vsg.dat$dem.s[which(vsg.dat$pid7_sc_12 < .5)] <- ifelse(vsg.dat$pid7_sc_16[which(vsg.dat$pid7_sc_12 < .5)]
                                                      == vsg.dat$pid7_sc_12[which(vsg.dat$pid7_sc_12 < .5)], 1, 0)
vsg.dat$dem.l <- NA
vsg.dat$dem.l[which(vsg.dat$pid7_sc_12 < .5)] <- ifelse(vsg.dat$pid7_sc_16[which(vsg.dat$pid7_sc_12 < .5)]
                                                      > vsg.dat$pid7_sc_12[which(vsg.dat$pid7_sc_12 < .5)] & 
                                                        vsg.dat$pid7_sc_16[which(vsg.dat$pid7_sc_12 < .5)] < .5, 1, 0)
vsg.dat$dem.ind <- NA
vsg.dat$dem.ind[which(vsg.dat$pid7_sc_12 < .5)] <- ifelse(vsg.dat$pid7_sc_16[which(vsg.dat$pid7_sc_12 < .5)] == .5, 1, 0)
vsg.dat$dem.rep <- NA
vsg.dat$dem.rep[which(vsg.dat$pid7_sc_12 < .5)] <- ifelse(vsg.dat$pid7_sc_16[which(vsg.dat$pid7_sc_12 < .5)] > .5, 1, 0)


vsg.dat$rep.m <- NA
vsg.dat$rep.m[which(vsg.dat$pid7_sc_12 > .5)] <- ifelse(vsg.dat$pid7_sc_16[which(vsg.dat$pid7_sc_12 > .5)]
                                                      > vsg.dat$pid7_sc_12[which(vsg.dat$pid7_sc_12 > .5)], 1, 0)
vsg.dat$rep.s <- NA
vsg.dat$rep.s[which(vsg.dat$pid7_sc_12 > .5)] <- ifelse(vsg.dat$pid7_sc_16[which(vsg.dat$pid7_sc_12 > .5)]
                                                      == vsg.dat$pid7_sc_12[which(vsg.dat$pid7_sc_12 > .5)], 1, 0)
vsg.dat$rep.l <- NA
vsg.dat$rep.l[which(vsg.dat$pid7_sc_12 > .5)] <- ifelse(vsg.dat$pid7_sc_16[which(vsg.dat$pid7_sc_12 > .5)]
                                                      < vsg.dat$pid7_sc_12[which(vsg.dat$pid7_sc_12 > .5)] & 
                                                        vsg.dat$pid7_sc_16[which(vsg.dat$pid7_sc_12 > .5)] > .5, 1, 0)
vsg.dat$rep.ind <- NA
vsg.dat$rep.ind[which(vsg.dat$pid7_sc_12 > .5)] <- ifelse(vsg.dat$pid7_sc_16[which(vsg.dat$pid7_sc_12 > .5)] == .5, 1, 0)
vsg.dat$rep.dem <- NA
vsg.dat$rep.dem[which(vsg.dat$pid7_sc_12 > .5)] <- ifelse(vsg.dat$pid7_sc_16[which(vsg.dat$pid7_sc_12 > .5)] < .5, 1, 0)

vsg.dat$ind.d <- NA
vsg.dat$ind.d[which(vsg.dat$pid7_sc_12 == .5)] <- ifelse(vsg.dat$pid7_sc_16[which(vsg.dat$pid7_sc_12 == .5)]
                                                       < vsg.dat$pid7_sc_12[which(vsg.dat$pid7_sc_12 == .5)], 1, 0)
vsg.dat$ind.r <- NA
vsg.dat$ind.r[which(vsg.dat$pid7_sc_12 == .5)] <- ifelse(vsg.dat$pid7_sc_16[which(vsg.dat$pid7_sc_12 == .5)]
                                                       > vsg.dat$pid7_sc_12[which(vsg.dat$pid7_sc_12 == .5)], 1, 0)
vsg.dat$ind.s <- NA
vsg.dat$ind.s[which(vsg.dat$pid7_sc_12 == .5)] <- ifelse(vsg.dat$pid7_sc_16[which(vsg.dat$pid7_sc_12 == .5)]
                                                       == vsg.dat$pid7_sc_12[which(vsg.dat$pid7_sc_12 == .5)], 1, 0)


## Get counts for each of the RR quantiles
vsg.dat$rr_sc_12.6 <- NA
vsg.dat$rr_sc_12.6[which(vsg.dat$rr_sc_12 <= 0.125)] <- 1
vsg.dat$rr_sc_12.6[which(vsg.dat$rr_sc_12 > 0.125 & vsg.dat$rr_sc_12 <= 0.3125)] <- 2
vsg.dat$rr_sc_12.6[which(vsg.dat$rr_sc_12 > 0.3125 & vsg.dat$rr_sc_12 <= 0.5)] <- 3
vsg.dat$rr_sc_12.6[which(vsg.dat$rr_sc_12 > 0.5 & vsg.dat$rr_sc_12 <= 0.6875)] <- 4
vsg.dat$rr_sc_12.6[which(vsg.dat$rr_sc_12 > 0.6875 & vsg.dat$rr_sc_12 <= 0.875)] <- 5
vsg.dat$rr_sc_12.6[which(vsg.dat$rr_sc_12 > 0.875)] <- 6
vsg.dat$rr_sc_16.6 <- NA
vsg.dat$rr_sc_16.6[which(vsg.dat$rr_sc_16 <= 0.125)] <- 1
vsg.dat$rr_sc_16.6[which(vsg.dat$rr_sc_16 > 0.125 & vsg.dat$rr_sc_16 <= 0.3125)] <- 2
vsg.dat$rr_sc_16.6[which(vsg.dat$rr_sc_16 > 0.3125 & vsg.dat$rr_sc_16 <= 0.5)] <- 3
vsg.dat$rr_sc_16.6[which(vsg.dat$rr_sc_16 > 0.5 & vsg.dat$rr_sc_16 <= 0.6875)] <- 4
vsg.dat$rr_sc_16.6[which(vsg.dat$rr_sc_16 > 0.6875 & vsg.dat$rr_sc_16 <= 0.875)] <- 5
vsg.dat$rr_sc_16.6[which(vsg.dat$rr_sc_16 > 0.875)] <- 6

vsg.dat$rr.s <- NA
vsg.dat$rr.s <- ifelse(vsg.dat$rr_sc_16.6 == vsg.dat$rr_sc_12.6, 1, 0)
vsg.dat$rr.l1 <- NA
vsg.dat$rr.l1 <- ifelse((vsg.dat$rr_sc_16.6 - vsg.dat$rr_sc_12.6) == -1, 1, 0)
vsg.dat$rr.l2 <- NA
vsg.dat$rr.l2 <- ifelse((vsg.dat$rr_sc_16.6 - vsg.dat$rr_sc_12.6) == -2, 1, 0)
vsg.dat$rr.l3 <- NA
vsg.dat$rr.l3 <- ifelse((vsg.dat$rr_sc_16.6 - vsg.dat$rr_sc_12.6) <= -3, 1, 0)
vsg.dat$rr.m1 <- NA
vsg.dat$rr.m1 <- ifelse((vsg.dat$rr_sc_16.6 - vsg.dat$rr_sc_12.6) == 1, 1, 0)
vsg.dat$rr.m2 <- NA
vsg.dat$rr.m2 <- ifelse((vsg.dat$rr_sc_16.6 - vsg.dat$rr_sc_12.6) == 2, 1, 0)
vsg.dat$rr.m3 <- NA
vsg.dat$rr.m3 <- ifelse((vsg.dat$rr_sc_16.6 - vsg.dat$rr_sc_12.6) >= 3, 1, 0)

### Creating Counts Table
party <- c("dem.m", "ind.d", "rep.dem", "rep.ind", "rep.l", "dem.s", "ind.s", "rep.s", "dem.l", 
           "dem.ind", "dem.rep", "ind.r", "rep.m")
# Takes Care of rows 1:6; iterate over "party" vector
COUNTS <- array(0, c(length(party), 12))
rownames(COUNTS) <- party
DF <- subset(vsg.dat)
for(i in 1:length(party)){
  VAR <- grep(party[i], names(DF), fixed = T)
  D <- DF[which(DF[,VAR] == 1 & DF$rr.s == 1),]
  COUNTS[i,4] <- table(D$rr_sc_12.6)["1"]
  COUNTS[i,5] <- table(D$rr_sc_12.6)["2"]
  COUNTS[i,6] <- table(D$rr_sc_12.6)["3"]
  COUNTS[i,7] <- table(D$rr_sc_12.6)["4"]
  COUNTS[i,8] <- table(D$rr_sc_12.6)["5"]
  COUNTS[i,9] <- table(D$rr_sc_12.6)["6"]
  D <- DF[which(DF[,VAR] == 1 & DF$rr.l1 == 1),]
  COUNTS[i,3] <- nrow(D)
  D <- DF[which(DF[,VAR] == 1 & DF$rr.l2 == 1),]
  COUNTS[i,2] <- nrow(D)
  D <- DF[which(DF[,VAR] == 1 & DF$rr.l3 == 1),]
  COUNTS[i,1] <- nrow(D)
  D <- DF[which(DF[,VAR] == 1 & DF$rr.m1 == 1),]
  COUNTS[i,10] <- nrow(D)
  D <- DF[which(DF[,VAR] == 1 & DF$rr.m2 == 1),]
  COUNTS[i,11] <- nrow(D)
  D <- DF[which(DF[,VAR] == 1 & DF$rr.m3 == 1),]
  COUNTS[i,12] <- nrow(D)
}
COUNTS[which(is.na(COUNTS))] <- 0

#----------------------------------------------------------------------------------------------------#
# CCAP 2016 ----
#----------------------------------------------------------------------------------------------------#
## Get counts for each of the PID Groups
dem.m <- sum(table(ccap16$b_pid7, ccap16$p_pid7)[2:3,1]) + 
  sum(table(ccap16$b_pid7, ccap16$p_pid7)[3,2])
dem.s <- sum(table(ccap16$b_pid7, ccap16$p_pid7)[1,1]) +
  sum(table(ccap16$b_pid7, ccap16$p_pid7)[2,2]) +
  sum(table(ccap16$b_pid7, ccap16$p_pid7)[3,3])
dem.l <- sum(table(ccap16$b_pid7, ccap16$p_pid7)[1,2:3]) +
  sum(table(ccap16$b_pid7, ccap16$p_pid7)[2,3])
dem.ind <- sum(table(ccap16$b_pid7, ccap16$p_pid7)[1:3,4])
dem.rep <- sum(table(ccap16$b_pid7, ccap16$p_pid7)[1:3,5:7])

ind.d <- sum(table(ccap16$b_pid7, ccap16$p_pid7)[4,1:3])
ind.s <- sum(table(ccap16$b_pid7, ccap16$p_pid7)[4,4])
ind.r <- sum(table(ccap16$b_pid7, ccap16$p_pid7)[4,5:7])

rep.m <- sum(table(ccap16$b_pid7, ccap16$p_pid7)[5,6:7]) + 
  sum(table(ccap16$b_pid7, ccap16$p_pid7)[6,7])
rep.s <- sum(table(ccap16$b_pid7, ccap16$p_pid7)[5,5]) +
  sum(table(ccap16$b_pid7, ccap16$p_pid7)[6,6]) +
  sum(table(ccap16$b_pid7, ccap16$p_pid7)[7,7])
rep.l <- sum(table(ccap16$b_pid7, ccap16$p_pid7)[6:7,5]) +
  sum(table(ccap16$b_pid7, ccap16$p_pid7)[7,6])
rep.ind <- sum(table(ccap16$b_pid7, ccap16$p_pid7)[5:7,4])
rep.dem <- sum(table(ccap16$b_pid7, ccap16$p_pid7)[5:7,1:3])

ccap16$dem.m <- NA
ccap16$dem.m[which(ccap16$b_pid7 < .5)] <- ifelse(ccap16$p_pid7[which(ccap16$b_pid7 < .5)]
                                                   < ccap16$b_pid7[which(ccap16$b_pid7 < .5)], 1, 0)
ccap16$dem.s <- NA
ccap16$dem.s[which(ccap16$b_pid7 < .5)] <- ifelse(ccap16$p_pid7[which(ccap16$b_pid7 < .5)]
                                                   == ccap16$b_pid7[which(ccap16$b_pid7 < .5)], 1, 0)
ccap16$dem.l <- NA
ccap16$dem.l[which(ccap16$b_pid7 < .5)] <- ifelse(ccap16$p_pid7[which(ccap16$b_pid7 < .5)]
                                                   > ccap16$b_pid7[which(ccap16$b_pid7 < .5)] & 
                                                     ccap16$p_pid7[which(ccap16$b_pid7 < .5)] < .5, 1, 0)
ccap16$dem.ind <- NA
ccap16$dem.ind[which(ccap16$b_pid7 < .5)] <- ifelse(ccap16$p_pid7[which(ccap16$b_pid7 < .5)] == .5, 1, 0)
ccap16$dem.rep <- NA
ccap16$dem.rep[which(ccap16$b_pid7 < .5)] <- ifelse(ccap16$p_pid7[which(ccap16$b_pid7 < .5)] > .5, 1, 0)


ccap16$rep.m <- NA
ccap16$rep.m[which(ccap16$b_pid7 > .5)] <- ifelse(ccap16$p_pid7[which(ccap16$b_pid7 > .5)]
                                                   > ccap16$b_pid7[which(ccap16$b_pid7 > .5)], 1, 0)
ccap16$rep.s <- NA
ccap16$rep.s[which(ccap16$b_pid7 > .5)] <- ifelse(ccap16$p_pid7[which(ccap16$b_pid7 > .5)]
                                                   == ccap16$b_pid7[which(ccap16$b_pid7 > .5)], 1, 0)
ccap16$rep.l <- NA
ccap16$rep.l[which(ccap16$b_pid7 > .5)] <- ifelse(ccap16$p_pid7[which(ccap16$b_pid7 > .5)]
                                                   < ccap16$b_pid7[which(ccap16$b_pid7 > .5)] & 
                                                     ccap16$p_pid7[which(ccap16$b_pid7 > .5)] > .5, 1, 0)
ccap16$rep.ind <- NA
ccap16$rep.ind[which(ccap16$b_pid7 > .5)] <- ifelse(ccap16$p_pid7[which(ccap16$b_pid7 > .5)] == .5, 1, 0)
ccap16$rep.dem <- NA
ccap16$rep.dem[which(ccap16$b_pid7 > .5)] <- ifelse(ccap16$p_pid7[which(ccap16$b_pid7 > .5)] < .5, 1, 0)

ccap16$ind.d <- NA
ccap16$ind.d[which(ccap16$b_pid7 == .5)] <- ifelse(ccap16$p_pid7[which(ccap16$b_pid7 == .5)]
                                                    < ccap16$b_pid7[which(ccap16$b_pid7 == .5)], 1, 0)
ccap16$ind.r <- NA
ccap16$ind.r[which(ccap16$b_pid7 == .5)] <- ifelse(ccap16$p_pid7[which(ccap16$b_pid7 == .5)]
                                                    > ccap16$b_pid7[which(ccap16$b_pid7 == .5)], 1, 0)
ccap16$ind.s <- NA
ccap16$ind.s[which(ccap16$b_pid7 == .5)] <- ifelse(ccap16$p_pid7[which(ccap16$b_pid7 == .5)]
                                                    == ccap16$b_pid7[which(ccap16$b_pid7 == .5)], 1, 0)


## Get counts for each of the RR quantiles
ccap16$b_rr_sc6 <- NA
ccap16$b_rr_sc6[which(ccap16$b_rr_sc <= 0.125)] <- 1
ccap16$b_rr_sc6[which(ccap16$b_rr_sc > 0.125 & ccap16$b_rr_sc <= 0.3125)] <- 2
ccap16$b_rr_sc6[which(ccap16$b_rr_sc > 0.3125 & ccap16$b_rr_sc <= 0.5)] <- 3
ccap16$b_rr_sc6[which(ccap16$b_rr_sc > 0.5 & ccap16$b_rr_sc <= 0.6875)] <- 4
ccap16$b_rr_sc6[which(ccap16$b_rr_sc > 0.6875 & ccap16$b_rr_sc <= 0.875)] <- 5
ccap16$b_rr_sc6[which(ccap16$b_rr_sc > 0.875)] <- 6
ccap16$p_rr_sc6 <- NA
ccap16$p_rr_sc6[which(ccap16$p_rr_sc <= 0.125)] <- 1
ccap16$p_rr_sc6[which(ccap16$p_rr_sc > 0.125 & ccap16$p_rr_sc <= 0.3125)] <- 2
ccap16$p_rr_sc6[which(ccap16$p_rr_sc > 0.3125 & ccap16$p_rr_sc <= 0.5)] <- 3
ccap16$p_rr_sc6[which(ccap16$p_rr_sc > 0.5 & ccap16$p_rr_sc <= 0.6875)] <- 4
ccap16$p_rr_sc6[which(ccap16$p_rr_sc > 0.6875 & ccap16$p_rr_sc <= 0.875)] <- 5
ccap16$p_rr_sc6[which(ccap16$p_rr_sc > 0.875)] <- 6

ccap16$rr.s <- NA
ccap16$rr.s <- ifelse(ccap16$p_rr_sc6 == ccap16$b_rr_sc6, 1, 0)
ccap16$rr.l1 <- NA
ccap16$rr.l1 <- ifelse((ccap16$p_rr_sc6 - ccap16$b_rr_sc6) == -1, 1, 0)
ccap16$rr.l2 <- NA
ccap16$rr.l2 <- ifelse((ccap16$p_rr_sc6 - ccap16$b_rr_sc6) == -2, 1, 0)
ccap16$rr.l3 <- NA
ccap16$rr.l3 <- ifelse((ccap16$p_rr_sc6 - ccap16$b_rr_sc6) <= -3, 1, 0)
ccap16$rr.m1 <- NA
ccap16$rr.m1 <- ifelse((ccap16$p_rr_sc6 - ccap16$b_rr_sc6) == 1, 1, 0)
ccap16$rr.m2 <- NA
ccap16$rr.m2 <- ifelse((ccap16$p_rr_sc6 - ccap16$b_rr_sc6) == 2, 1, 0)
ccap16$rr.m3 <- NA
ccap16$rr.m3 <- ifelse((ccap16$p_rr_sc6 - ccap16$b_rr_sc6) >= 3, 1, 0)

### Creating Counts Table
party <- c("dem.m", "ind.d", "rep.dem", "rep.ind", "rep.l", "dem.s", "ind.s", "rep.s", "dem.l", 
           "dem.ind", "dem.rep", "ind.r", "rep.m")
# Takes Care of rows 1:6; iterate over "party" vector
COUNTS <- array(0, c(length(party), 12))
rownames(COUNTS) <- party
DF <- subset(ccap16)
for(i in 1:length(party)){
  VAR <- grep(party[i], names(DF), fixed = T)
  D <- DF[which(DF[,VAR] == 1 & DF$rr.s == 1),]
  COUNTS[i,4] <- table(D$b_rr_sc6)["1"]
  COUNTS[i,5] <- table(D$b_rr_sc6)["2"]
  COUNTS[i,6] <- table(D$b_rr_sc6)["3"]
  COUNTS[i,7] <- table(D$b_rr_sc6)["4"]
  COUNTS[i,8] <- table(D$b_rr_sc6)["5"]
  COUNTS[i,9] <- table(D$b_rr_sc6)["6"]
  D <- DF[which(DF[,VAR] == 1 & DF$rr.l1 == 1),]
  COUNTS[i,3] <- nrow(D)
  D <- DF[which(DF[,VAR] == 1 & DF$rr.l2 == 1),]
  COUNTS[i,2] <- nrow(D)
  D <- DF[which(DF[,VAR] == 1 & DF$rr.l3 == 1),]
  COUNTS[i,1] <- nrow(D)
  D <- DF[which(DF[,VAR] == 1 & DF$rr.m1 == 1),]
  COUNTS[i,10] <- nrow(D)
  D <- DF[which(DF[,VAR] == 1 & DF$rr.m2 == 1),]
  COUNTS[i,11] <- nrow(D)
  D <- DF[which(DF[,VAR] == 1 & DF$rr.m3 == 1),]
  COUNTS[i,12] <- nrow(D)
}
COUNTS[which(is.na(COUNTS))] <- 0